package com.qqt.csr.order.service.statemachine.condition;

import com.alibaba.cola.statemachine.Condition;
import com.qqt.csr.order.service.statemachine.context.WorkOrderContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author
 */
@Slf4j
@Component
public class DefaultWorkOrderCondition implements Condition<WorkOrderContext> {
    @Autowired(required = false)
    private List<ISubCondition> conditionList;

    @Override
    public boolean isSatisfied(WorkOrderContext context) {
        if (CollectionUtils.isEmpty(conditionList)) {
            return true;
        }
        return conditionList
                .stream()
                .filter(condition -> condition.matches(context.getEvent()))
                .allMatch(condition -> condition.isSatisfied(context));
    }
}
